'''
强制函数签名
'''

from inspect import Signature, Parameter
import inspect

def make_sig(*names):
    parms = [Parameter(name, Parameter.POSITIONAL_OR_KEYWORD)
            for name in names]
    return Signature(parms)

class Structure:
    __signature__ = make_sig()
    def __init__(self, *args, **kwargs):
        bound_values = self.__signature__.bind(*args, **kwargs)
        for name, value in bound_values.arguments.items():
            setattr(self, name, value)

# Example use
class Stock(Structure):
    __signature__ = make_sig('name', 'shares', 'price')

class Point(Structure):
    __signature__ = make_sig('x', 'y')

def recipe1():
    print(f"{inspect.signature(Stock) = }")  

    s1 = Stock('ACME', 100, 490.1)
    print(f"{s1 = }")  

    try:
        Stock('ACME', 100)
    except TypeError as e:
        print(e)     
    
    try:
        Stock('ACME', 100, 490.1, shares=50)
    except TypeError as e:
        print(e)  


def main():
    print('recipe1'.center(20, '*'))
    recipe1()

if __name__ == '__main__':
    main()                